package demo.practice.leetcode_offer;

import lombok.extern.slf4j.Slf4j;

import java.util.*;


@Slf4j
public class P15 {


    public static void main(String[] args) {
        P15 p15 = new P15();
        p15.threeSum(new int[]{-1, 0, 1, 2, -1, -4});
    }

    public List<List<Integer>> threeSum(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }

        List<List<Integer>> list = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                int m = -(nums[i] + nums[j]);
                if (map.containsKey(m)) {
                    if(m!=nums[i] && m!=nums[j])
                        list.add(Arrays.asList(nums[i], nums[j], -(nums[i] + nums[j])));
                }
            }
        }
        return list;
    }
}
